//
//  SLOrderListContainerViewController.m
//  SulitLoanDemo
//
//  Created by 虞嘉伟 on 2025/2/20.
//


#import "SLOrderListContainerViewController.h"
#import <YYKit.h>
#import <Masonry.h>
#import <TYPagerController/TYTabPagerBar.h>
#import <TYPagerController/TYPagerController.h>
#import "SLOrderListViewController.h"

@interface SLOrderListTabBarCell : TYTabPagerBarCell
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UILabel *textLabel;
@end
@implementation SLOrderListTabBarCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.bgView = [UIView new];
        self.bgView.layer.borderColor = UIColorHex(#1B1B1B).CGColor;
        self.bgView.layer.borderWidth = 1;
        self.bgView.layer.cornerRadius = 20;
        [self.contentView addSubview:self.bgView];
        [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.contentView);
            make.top.bottom.equalTo(self.contentView);
        }];
        
        self.textLabel = [UILabel new];
        self.textLabel.font = aspectDefaultFont(14);
        self.textLabel.textAlignment = NSTextAlignmentCenter;
        [self.bgView addSubview:self.textLabel];
        [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.bgView);
        }];
    }
    return self;
}
+ (NSString *)cellIdentifier {
    return @"SLOrderListTabBarCell";
}
@end



@interface SLOrderListContainerViewController ()<TYTabPagerBarDataSource, TYTabPagerBarDelegate, TYPagerControllerDataSource, TYPagerControllerDelegate>
@property (nonatomic, strong) TYTabPagerBar *tabBar;
@property (nonatomic, strong) TYPagerController *pagerController;

@property (nonatomic, copy  ) NSArray<NSString *> *titleArray;
@property (nonatomic, strong) NSMutableArray<UIViewController *> *ctrlArray;

@end

@implementation SLOrderListContainerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self showNavBar:@"Order List"];
    
    self.titleArray = @[@"All Order", @"Under Order", @"Pending Repayment", @"Completed"];
    self.ctrlArray = [NSMutableArray array];
    for (NSString *title in self.titleArray) {
        SLOrderListViewController *ctrl = [SLOrderListViewController new];
        // 状态 4全部 7进行中 6待还款 5已结清
        if ([title isEqualToString:@"All Order"]) {
            ctrl.status = @"4";
        } else if ([title isEqualToString:@"Under Order"]) {
            ctrl.status = @"7";
        } else if ([title isEqualToString:@"Pending Repayment"]) {
            ctrl.status = @"6";
        } else if ([title isEqualToString:@"Completed"]) {
            ctrl.status = @"5";
        }
        [self.ctrlArray addObject:ctrl];
    }
    
    UIView *tabbarContainer = [UIView new];
    [self.view addSubview:tabbarContainer];
    tabbarContainer.backgroundColor = UIColor.whiteColor;
    [tabbarContainer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.navBar.mas_bottom);
        make.height.mas_equalTo(15*2+40);
    }];
    
    // 创建 TYTabPagerBar
    self.tabBar = [[TYTabPagerBar alloc] init];
    self.tabBar.dataSource = self;
    self.tabBar.delegate = self;
    self.tabBar.layout.normalTextFont = aspectDefaultFont(16);
    self.tabBar.layout.normalTextColor = UIColorHex(#1B1B1B);
    self.tabBar.layout.selectedTextFont = aspectDefaultFont(16);
    self.tabBar.layout.selectedTextColor = UIColorHex(#1B1B1B);
    self.tabBar.autoScrollItemToCenter = YES;
    [self.tabBar registerClass:[SLOrderListTabBarCell class] forCellWithReuseIdentifier:[SLOrderListTabBarCell cellIdentifier]];
    [self.view addSubview:self.tabBar];
    
    // 配置样式
    self.tabBar.layout.barStyle = TYPagerBarStyleNoneView;
    self.tabBar.layout.cellSpacing = 10;

    self.pagerController = [[TYPagerController alloc] init];
    self.pagerController.dataSource = self;
    self.pagerController.delegate = self;
    [self addChildViewController:self.pagerController];
    [self.view addSubview:self.pagerController.view];
    [self.pagerController didMoveToParentViewController:self];
    
    
    [self.tabBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(tabbarContainer);
        make.left.right.equalTo(self.view).inset(20);
        make.height.mas_equalTo(40);
    }];
    [self.pagerController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(tabbarContainer.mas_bottom);
    }];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.tabBar scrollToItemAtIndex:self.curIndex atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    [self.pagerController scrollToControllerAtIndex:self.curIndex animate:YES];
}

#pragma mark - TYTabPagerBarDataSource

- (NSInteger)numberOfItemsInPagerTabBar {
    return self.titleArray.count;
}

- (CGFloat)pagerTabBar:(TYTabPagerBar *)pagerTabBar widthForItemAtIndex:(NSInteger)index {
    NSString *title = self.titleArray[index];
    return [title boundingRectWithSize:CGSizeMake(MAXFLOAT, 40) options:0 attributes:nil context:nil].size.width + 20*2;
}
- (UICollectionViewCell<TYTabPagerBarCellProtocol> *)pagerTabBar:(TYTabPagerBar *)pagerTabBar cellForItemAtIndex:(NSInteger)index {
    SLOrderListTabBarCell *cell = [pagerTabBar dequeueReusableCellWithReuseIdentifier:[SLOrderListTabBarCell cellIdentifier] forIndex:index];
    cell.textLabel.text = self.titleArray[index];
    
    if (_curIndex == index) {
        cell.bgView.backgroundColor = UIColorHex(#FDC5E0);
        cell.textLabel.textColor = UIColorHex(#1B1B1B);
    } else {
        cell.bgView.backgroundColor = UIColorHex(#FFFFFF);
        cell.textLabel.textColor = UIColorHex(#1B1B1B);
    }
    return cell;
}

#pragma mark - TYTabPagerBarDelegate

- (void)pagerTabBar:(TYTabPagerBar *)pagerTabBar didSelectItemAtIndex:(NSInteger)index {
    self.curIndex = index;
    [pagerTabBar reloadData];
    [self.pagerController scrollToControllerAtIndex:index animate:YES];
}


#pragma mark - TYPagerControllerDataSource

- (NSInteger)numberOfControllersInPagerController {
    return self.ctrlArray.count;
}

- (UIViewController *)pagerController:(TYPagerController *)pagerController controllerForIndex:(NSInteger)index prefetching:(BOOL)prefetching {
    return self.ctrlArray[index];
}
#pragma mark - TYPagerControllerDelegate
- (void)pagerController:(TYPagerController *)pagerController transitionFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex animated:(BOOL)animated {
    self.curIndex = toIndex;
    [self.tabBar reloadData];
    [self.tabBar scrollToItemAtIndex:self.curIndex atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}

@end
